﻿@using OSBLE.Models.Assignments
@using OSBLE.Areas.AssignmentDetails.ViewModels
@using OSBLE.Models.Courses;
@using OSBLE.Resources
@model AssignmentDetailsViewModel
@{
    ViewBag.Title = "Assignment Details";
    DynamicDictionary headerData = Model.HeaderBuilder.BuildHeader(Model.CurrentAssignment);
    
}
@section AssignmentDetails
{
    <!--No need to pust due date here like other Indexes, as its covered in partial views as InitialFinalPostDueDate-->

    @foreach (string view in Model.HeaderViews)
    {
        string viewLocation = string.Format("HeaderBuilder/{0}", view);
        @Html.Partial(viewLocation, headerData);
    }
}
@section TableData
{
    @if (Model.Client.AbstractRole.CanGrade)
    {
        var teams = from t in Model.CurrentAssignment.DiscussionTeams
                    orderby t.Team.Name ascending
                    select t;
        

        foreach (DiscussionTeam assignmentTeam in teams)
        {
            if (Model.CurrentAssignment.HasDiscussionTeams)
            {
                <tr>
                    <th class="TeamHeader" colspan="6">

                        @{string actionLink = Url.Action("CreateTeam", "Mail", new { area = "", teamID = assignmentTeam.Team.ID });
                        }
                        <a href="@actionLink" title="Email this team" >@Helpers.MailButton()</a>
                        @assignmentTeam.Team.Name - 
                             @(Html.ActionLink("View Discussion",
                            "TeacherIndex", "DiscussionAssignment",
                             new
                             {
                                 assignmentId = Model.CurrentAssignment.ID,
                                 discussionTeamID = assignmentTeam.ID,
                                 area = ""
                             }, new { }))

                            @{
                                int newPosts = assignmentTeam.GetNewPostsCount(Model.Client.ID);
                              }
                            @if(newPosts > 0)
                            {
                                @:&nbsp-&nbsp
                                @:@Html.ActionLink("View " + newPosts.ToString() + " Unread Posts",
                                "TeacherIndex", "DiscussionAssignment",
                                 new
                                 {
                                     assignmentId = Model.CurrentAssignment.ID,
                                     courseUserId = 0,
                                     discussionTeamID = assignmentTeam.ID,
                                     hightlightValue = OSBLE.Controllers.DiscussionAssignmentController.HighlightValue.NewPosts,
                                     area = ""
                                 }, new { })
                            }
                        
                    </th>
                </tr>
            }
            
            foreach (TeamMember member in (assignmentTeam as DiscussionTeam).GetAllTeamMembers()
                .OrderBy(t => t.CourseUser.AbstractRoleID, new OSBLE.Models.Courses.CourseRole.CourseRolesInOrder())
                .ThenBy(t => t.CourseUser.DisplayName((int)CourseRole.CourseRoles.Instructor)))
            {
                DiscussionTeam singleUserAssignmentTeam = new DiscussionTeam();
                singleUserAssignmentTeam.Team = new Team();
                singleUserAssignmentTeam.Team.TeamMembers.Add(member);
                singleUserAssignmentTeam.Team.Name = member.CourseUser.DisplayName(Model.Client.AbstractRoleID, false);
                singleUserAssignmentTeam.TeamID = member.TeamID;
                singleUserAssignmentTeam.Assignment = assignmentTeam.Assignment;
                singleUserAssignmentTeam.AssignmentID = assignmentTeam.AssignmentID;
                singleUserAssignmentTeam.ID = (assignmentTeam as DiscussionTeam).ID;
                
                <tr>
                    <td>
                        @{string actionLink = Url.Action("CreateUser", "Mail", new { area = "", id = member.CourseUser.ID });
                        }
                        <a href="@actionLink" title="Email this user">@Helpers.MailButton()</a>
                        @if(member.CourseUser.AbstractRoleID == (int)CourseRole.CourseRoles.TA || member.CourseUser.AbstractRoleID == (int)CourseRole.CourseRoles.Moderator)
                        {
                            @:<text class="TitleText">@member.CourseUser.DisplayNameWithRole(Model.Client.AbstractRoleID, false)</text>
                        }
                        else
                        {
                            @:<text>@member.CourseUser.DisplayName(Model.Client.AbstractRoleID, false)</text>
                        }
                    </td>
                    @foreach (string view in Model.TableColumnHeaders.Keys)
                    {
                        DynamicDictionary tableData = Model.TeamTableBuilders[assignmentTeam].BuildTableForTeam(singleUserAssignmentTeam);
                        string viewLocation = string.Format("TableBuilder/{0}", view);
                        @Html.Partial(viewLocation, tableData);
                    }
                </tr>
            }
        }
    }
}
